package com.github.easyms.server.config.swagger;

import com.fasterxml.classmate.TypeResolver;
import com.github.easyms.common.constant.GlobalConstants;
import com.github.easyms.common.property.EasymsProperties;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * Swagger配置
 *
 * @author Leo.Liao
 * @date 2020-04-29 15:24:22
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(TypeResolver resolver, EasymsProperties easymsProperties) {

        ParameterBuilder tokeParameterBuilder = new ParameterBuilder();
        tokeParameterBuilder.name(HttpHeaders.AUTHORIZATION).description("数据接口访问令牌").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

        ParameterBuilder langParameterBuilder = new ParameterBuilder();
        String langParamDesc = "客户端语言配置,固定可选值zh_CN:简体中中文(默认);zh_TW:繁体中文(香港地区);zh_TW:繁体中文(台湾地区);en:英文";
        langParameterBuilder.name(HttpHeaders.ACCEPT_LANGUAGE).description(langParamDesc).modelRef(new ModelRef("string")).parameterType("header").defaultValue(easymsProperties.getDefaultLang()).required(true).build();

        List<Parameter> aParameters = Lists.newArrayList();
        aParameters.add(tokeParameterBuilder.build());
        aParameters.add(langParameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(aParameters)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.github.easyms.server.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("EasyMS接口文档")
                        .description("")
                        .version("1.0.0")
                        .contact(new Contact("","",""))
                        .license("")
                        .licenseUrl("")
                        .build());
    }

}
